<!DOCTYPE html>
<html>
    <head>
        <title>Test MediaKeySession closed event</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            async_test(function(test)
            {
                var initDataType;
                var initData;
                var mediaKeySession;

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    mediaKeySession = mediaKeys.createSession();
                    return mediaKeySession.generateRequest(initDataType, initData);
                }).then(function() {
                    // Wait for the session to be closed.
                    mediaKeySession.closed.then(function(reason) {
                        assert_equals(reason, "closed-by-application");
                        // Now that the session is closed, verify that the
                        // closed attribute immediately returns a fulfilled
                        // promise.
                        return mediaKeySession.closed;
                    }).then(function(reason) {
                        assert_equals(reason, "closed-by-application");
                        test.done();
                    });

                    // release() should result in the closed promise being
                    // fulfilled.
                    return mediaKeySession.close();
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'Test MediaKeySession closed event.');
        </script>
    </body>
</html>
